package com.itcg.springboot03deptemptest.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itcg.springboot03deptemptest.mapper.EmpMapper;
import com.itcg.springboot03deptemptest.pojo.*;
import com.itcg.springboot03deptemptest.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


@Service
@Slf4j
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    @Transactional
    public void updates(Emp emp) {
        empMapper.updates(emp);
    }

    @Override
    @Transactional
    public void deletes(Integer[] ids) {
        empMapper.deletes(ids);
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.findById(id);
    }

    @Override
    @Transactional
    public int inserts(Emp emp) {
        return empMapper.inserts(emp);
    }

    @Override
    public List<Emp> findEmpByDeptId(Integer id) {
        return empMapper.findByDeptId(id);
    }

    @Override
    public PageBean<Emp> findByCondition2(EmpQuery empQuery) {
        //设置当前页码
        Integer PageNum = empQuery.getPageNum();
        if (PageNum == null) {
            PageNum = 1;
        }
        //设置当前页面数据
        Integer PageSize = empQuery.getPageSize();
        if (PageSize == null) {
            PageSize = 3;
        }
        //使用插件
        PageHelper.startPage(PageNum, PageSize);
        //条件查询
        List<Emp> empList = empMapper.findByCondition2(empQuery);

        log.info("===>> {}", empList);
        PageInfo<Emp> info = new PageInfo<>(empList);
        //将数据塞进 empPage
        PageBean<Emp> empPage = new PageBean<>();
        empPage.setTotalCount(info.getTotal());
        empPage.setPageSize(PageSize);
        empPage.setPageNum(PageNum);
        empPage.setTotalPage(info.getPages());
        empPage.setDataList(empList);
        return empPage;
    }
}
